#include "Game.h"

Game::Game() {
	this->SCREEN_BPP = 32;
	this->SCREEN_HEIGHT = 720;
	this->SCREEN_WIDTH = 1280;
	this->WINDOW_CAPTION = "EVAD GAME TEST";
	this->map = NULL;
	this->player = NULL;
}

Game::~Game() {

}

bool Game::initComponents() {
	if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
		return false;
	}

	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
	if (screen == NULL) {
		return false;
	}
	this->rend = Renderer(screen);

	SDL_WM_SetCaption(WINDOW_CAPTION.c_str(), NULL);

	return true;
}

bool Game::handleInput() {
	while (SDL_PollEvent(&event)) {
		if (event.type == SDL_QUIT) {
			return false;
		}
	}

	Uint8* keyStates = SDL_GetKeyState(NULL);
	//********************  Debug controls, move screen around independent of character  ********************
	if (keyStates[SDLK_UP]) {
		if (keyStates[SDLK_LSHIFT]) {
			rend.setCenterPoint(rend.getCenterX(), rend.getCenterY() - 10);
		}
		else {
			rend.setCenterPoint(rend.getCenterX(), rend.getCenterY() - 1);
		}
	}
	if (keyStates[SDLK_DOWN]) {
		if (keyStates[SDLK_LSHIFT]) {
			rend.setCenterPoint(rend.getCenterX(), rend.getCenterY() + 10);
		}
		else {
			rend.setCenterPoint(rend.getCenterX(), rend.getCenterY() + 1);
		}
	}
	if (keyStates[SDLK_LEFT]) {
		if (keyStates[SDLK_LSHIFT]) {
			rend.setCenterPoint(rend.getCenterX() - 10, rend.getCenterY());
		}
		else {
			rend.setCenterPoint(rend.getCenterX() - 1, rend.getCenterY());
		}
	}
	if (keyStates[SDLK_RIGHT]) {
		if (keyStates[SDLK_LSHIFT]) {
			rend.setCenterPoint(rend.getCenterX() + 10, rend.getCenterY());
		}
		else {
			rend.setCenterPoint(rend.getCenterX() + 1, rend.getCenterY());
		}
	}

	//********************  Character movement controls  ********************
	if (keyStates[SDLK_w] || keyStates[SDLK_SPACE]) {
		player->jump();
	}
	if (keyStates[SDLK_a]) {
		player->setDirection(Renderable::LEFT);
	}
	if (keyStates[SDLK_d]) {
		player->setDirection(Renderable::RIGHT);
	}

	return true;
}

void Game::uninitComponents() {
	SDL_Quit();
}

bool Game::loadContent() {
	SDL_Surface* tileSheet = rend.loadImage("Resources/Images/TileSet_Checkerboard.png");
	map = new Map("Resources/Maps/TestMap.info", "Resources/Maps/TestMap.map", tileSheet);
	if (map->isGood() == false) {
		return false;
	}

	entities = map->getEntityList();
	if (entities[0]->getName() == "Player_Start") {
		player = (Player*)entities[0];
	}
	if (player == NULL) {
		return false;
	}
	player->setAnimation(new SpriteAnimation(64, 64, 100, rend.loadImage("Resources/Images/Player_Test.png")));

	//set center to player position
	rend.setCenterPoint((int)player->getXPos(), (int)player->getYPos());

	//Load temp objects for testing
	return true;
}

void Game::unloadContent() {
	delete map;
}

void Game::update(Uint32 delta) {
	//update the entities
	player->update(delta);

	//check for collisions with the map
	Renderable::Direction dir;
	if ((dir = map->checkMapCollision(player)) != Renderable::NONE) {
		player->handleCollision(dir);
	}

	//Make camera follow player
	rend.setCenterPoint((int)player->getXPos(), (int)player->getYPos());
}

void Game::draw() {
	//Clear screen before drawing
	SDL_FillRect(screen,NULL, 0x000000);

	rend.draw(map);
	rend.draw(player);
	//Update the screen
	SDL_Flip(screen);
}
